/*
*  Openmysee
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*/
#include "stdafx.h"
#include "LogMgr.h"

LogMgr::LogMgr() : fp(NULL) {
	m_bPrintTime = TRUE;
	m_tBuf[0] = 0;
	Init();
}

LogMgr::~LogMgr() {
	Uninit();
}

void LogMgr::Init() {
	char buf[MAX_PATH+1];
    DWORD res = ::GetModuleFileName(NULL, buf, MAX_PATH);
	m_csFileName = buf;

    m_csFileName.resize(m_csFileName.find_last_of('\\')+1);

	RemoveOldTmpFile(buf);

	sprintf(buf, "#CL%d.tmp", GetTickCount());
	m_csFileName.append(buf);

	fp = fopen(m_csFileName.data(), "w");
}

void LogMgr::Uninit() {
	if(fp)
		fclose(fp);
	DeleteFile(m_csFileName.data());
}

void LogMgr::StatusOut(const char* fmt, ...) {
#ifdef NDEBUG
	return;
#endif

	if(!fp || !fmt)
		return;

	m_tBuf[0] = 0;

	if(m_bPrintTime) {
		struct tm *newtime;
		time_t long_time;

		time( &long_time );                /* Get time as long integer. */
		newtime = localtime( &long_time ); /* Convert to local time. */

		sprintf(m_tBuf, "%.8s: ", asctime(newtime)+11);
	}

	// parse that string format
	try {
		va_list argptr;
		va_start(argptr, fmt);
		_vsnprintf(m_tBuf+strlen(m_tBuf), TBUF_SIZE, fmt, argptr);
		va_end(argptr);

#ifdef _DEBUG
		printf("%s\n", m_tBuf);
#endif
		fprintf(fp, "%s\n", m_tBuf);
		fflush(fp);
	}
	catch(...) {
		m_tBuf[0] = 0;
	}
}

void LogMgr::StatusErr(const char* title, int errcode) {
#ifdef NDEBUG
	return;
#endif

	LPVOID lpMsgBuf;
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | 
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		errcode,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,
		0,
		NULL 
	);
	StatusOut("%s. Error(%d): %s", title, errcode, (LPCTSTR)lpMsgBuf);
	LocalFree(lpMsgBuf);
}

void LogMgr::RemoveOldTmpFile(const char* tmpPath) {
	if(!tmpPath)
		return;

	WIN32_FIND_DATA fileData;

	string match = tmpPath;
	match.append("#CL*.tmp");
	HANDLE hFind = FindFirstFile(match.data(), &fileData);
	if(hFind == INVALID_HANDLE_VALUE)
		return;

	while(1) {
		string path = tmpPath;
		path.append(fileData.cFileName);
		DeleteFile(path.data());

		if(!FindNextFile(hFind, &fileData)) {
			if(GetLastError() == ERROR_NO_MORE_FILES)
				break;
			else
				return; 
		}
	}
	FindClose(hFind);
}